<template>
  <a-modal title="通道管理" :width="640" :visible="visible" :confirmLoading="confirmLoading" :maskClosable="false" @ok="handleSubmit"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道名称">
          <a-input v-decorator="['name', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道标识">
          <a-input v-decorator="['payCode', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道账号">
          <a-input v-decorator="['account', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道秘钥">
          <a-input v-decorator="['password', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="通道Url">
          <a-input v-decorator="['apiUrl', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上游通道标识">
          <a-input v-decorator="['payType', {rules: []}]"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="适配方法名称">
          <a-input v-decorator="['methodName', {rules: [{required: true, message: '必输'}]}]"></a-input>
        </a-form-item>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { saveOrUpdate } from '@/api/shop/passageway'
  /**
   * 数据
   */
  const data = function() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      id: null,
      visible: false,
      confirmLoading: false,
      isDisabled: false,
      form: this.$form.createForm(this)
    }
  }

  const created = function() {

  }

  export default {
    data: data,
    created: created,
    /**
     * 方法
     */
    methods: {
      add () {
        this.visible = true
      },
      edit (record) {
        this.visible = true
        this.id = record.id
        this.isDisabled = true
        const { form: { setFieldsValue } } = this
        this.$nextTick(() => {
          const formData = pick(record, ['payCode','name','account','password','apiUrl','payType','methodName'])
          setFieldsValue(formData)
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.id = this.id
            saveOrUpdate(values).then((res) => {
              this.confirmLoading = false
              if (res.code === 200) {
                this.handleCancel()
                this.$emit('ok')
              }
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.id = null
        this.visible = false
        this.isDisabled = false
        this.confirmLoading = false
        this.form.resetFields()
      },
      filterOption (input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },

    },
  }
</script>